package com.howie.hmvc.paramparse;

import java.lang.annotation.Annotation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.howie.hmvc.annotations.Valid;
import com.howie.hmvc.exception.ArguErrorException;

public class ParamCharts extends AbsParamParse{

	private boolean required;
	
	@Override
	public void init(Annotation[] an) throws Exception {
		Valid valid = getValid(an);
		if(valid != null){
			required = valid.required();
		}
	}
	
	@Override
	public Object getParamter(HttpServletRequest request,HttpServletResponse response)throws Exception{
		String[] values = request.getParameterValues(name);
		if(values == null){
			if(required){
				throw new ArguErrorException(name,"required",null);
			}
		}else{
			Character[] vals = new Character[values.length];
			for(int i=0;i<values.length;i++){
				if(values[i] == null || values[i].length() == 0){
					if(required){
						throw new ArguErrorException(name,"required",null);
					}
				}else{
					vals[i] = values[i].charAt(0);
				}
			}
			return vals;
		}
		return null;
	}
	
}
